<%@page language="java" pageEncoding="utf-8"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="inet.util.StringTool"%>
<%@page import="lib.db.clip.ClipCategory"%>
<%@page import="lib.db.clip.MessageShow"%>

<jsp:useBean id="categoryDAO" class="lib.db.clip.ClipCategoryDAO" scope="session"/>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Cập nhật album</title>
    <link rel="icon" href="<%=request.getContextPath() %>/favicon.ico" type="image/x-icon" />
    <link href="../include/default.css" type="text/css" rel="stylesheet"></link>
	<script src="../include/default.js" type="text/javascript"></script>  
  </head>
  <body marginheight="0" topmargin="0" marginwidth="0" leftmargin="0" rightmargin="0">&nbsp; 
  	<%@ include file="../include/checklogin_not_menu.jsp" %>
  	<center>
	  	<p align="center">
			<b><font color="#000080" face="Tahoma" size="4">
				SỬA DANH MỤC
			</font></b><br/>
			<span class="list"><font color="red">(*)</font>: Bắt buộc nhập</span>
	  	</p>
		<br/>
		<%
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			String sAction = request.getParameter("action");
			String sCateName = request.getParameter("txtCateName");
			String sPosition = request.getParameter("txtPosition");
			String msg = "";
			int position = 0;
			if(StringTool.isNumberic(sPosition)) {
			     position = Integer.parseInt(sPosition);
			}
			String sCateId = request.getParameter("id");
			BigDecimal cateId = BigDecimal.ZERO;
			if(StringTool.isNumberic(sCateId)) {
				cateId = new BigDecimal(sCateId);
			}
			ClipCategory cate = categoryDAO.getRow(cateId);
			if(cate == null) {
			%>
				<script type="text/javascript">
					window.location.href = "<%=request.getContextPath() %>/message/?c=<%=MessageShow.CONTENT_OWNER %>&b=clip/cate_list.jsp";
				</script>
			<%
				return;
			} 
	    	
			if("EDIT_CATE".equalsIgnoreCase(sAction)){
				if(sCateName != null && !"".equalsIgnoreCase(sCateName)) {
					if(categoryDAO.updateRow(cate.getId(), sCateName, position) ){
						msg = "<span style='color:blue'>Cập nhật danh mục thành công</span>";
					} else {
						msg = "<span style='color:red'>Cập nhật danh mục KHÔNG thành công</span>";
					}
				}
				out.println("Đang chuyển hướng .... <script type=\"text/javascript\">setInterval(function redirect(){window.close();window.opener.location.reload(true);},3000);</script>");
			}
		%>
		<div align="center"><%=msg %></div><br/>
		<% 
			if(cate != null) {
		%>
		<form method="post" name="frmEditCate" onsubmit="return editCate();">
			<input type="hidden" name="action" />
	 			
			<table width="100%" border="0" cellpadding="4" cellspacing="0">
				<tr height="40px">
					<td class="header" width="40%">
					 	Tên danh mục
					</td>
					<td> 
					  <input type="text" size="30" name="txtCateName" value="<%= cate.getName() %>" maxlength="100"/>
					</td>
				</tr>
				<tr height="40px">
					<td class="header" width="40%">
					 	Vị trí
					</td>
					<td> 
					  <input type="text" size="30" name="txtPosition" value="<%= cate.getPosition() %>" maxlength="100"/>
					</td>
				</tr>
			</table>
			<table width="50%" border="0" rules="all" cellpadding="4" cellspacing="0">
				<tr height="40px">
					<td align="center">
						<input type="submit" class="blogg-button GEE3RVNDNU" class="flatbutton" value=" Cập nhật " />
					</td>
				</tr>
			</table>
		</form>
		<% } %>
    </center>
  </body>
  <script type="text/javascript">
  	function editCate(){
  		//Họ tên
  		if(frmEditCate.txtCateName.value == ''){
  			alert('Vui lòng nhập tên danh mục');
  			frmEditCate.txtCateName.focus();
  			return false;
  		}
  		
  		frmEditCate.action.value = 'EDIT_CATE';
  		frmEditCate.submit();
  	}
  	
  </script>
</html>
